/**
 * Created by qyy on 2018/8/12.
 */


var socketId = "";

function onRadioValueChanged(name)
{
    var radios = document.getElementsByName(name);
    var isSelectTrue = false;
    if(radios[0].checked == true)
    {
        // 如果是第一个radio被选中
        if(radios[0].value == "1")
        {
            isSelectTrue = true;
        }
        else
        {
            isSelectTrue = false;
        }
    }
    else
    {
        // 如果是第二个radio被选中
        if(radios[1].value == "1")
        {
            isSelectTrue = true;
        }
        else
        {
            isSelectTrue = false;
        }
    }

    if(isSelectTrue)
    {

        if(name == "needUploadSound")
        {
            // 如果是是否上传
            document.getElementById("sound-ids").style.display = "inline";
        }
        else  if(name == "needUploadRecourd")
        {
            // 如果是是否上传
            document.getElementById("record-date").style.display = "inline";
        }

    }
    else
    {
        if(name == "needUploadSound")
        {
            // 如果是是否上传
            document.getElementById("sound-ids").style.display = "none";
        }
        else  if(name == "needUploadRecourd")
        {
            // 如果是是否上传
            document.getElementById("record-date").style.display = "none";
        }
    }
}

function insertNewCommand()
{
    var oParams =
    {
        needuploadlocation : $('input[name="needUploadLocation"]:checked').val(),
        needuploadrecords : $('input[name="needUploadRecourd"]:checked').val() ,
        needuploadsounds : $('input[name="needUploadSound"]:checked').val() ,
        recordsbegindate : $('input[name="beginDate"]').val(),
        recordsenddate :  $('input[name="endDate"]').val(),
        uploadsoundsids :  $('#sound-ids').val()
    };
    if(oParams.needuploadrecords == "1" && $.trim(oParams.recordsbegindate) == "")
    {
        alert('请输入上传通话记录起始日期');
        return;
    }

    if(oParams.needuploadrecords == "1"&& $.trim(oParams.recordsenddate) == "")
    {
        alert('请输入上传通话记录终止日期');
        return;
    }
    if(oParams.needuploadsounds == "1"&& $.trim(oParams.uploadsoundsids) == "")
    {
        alert('请输入上传通话录音编号');
        return;
    }
    var sParams = JSON.stringify( oParams ) ;
    var sUrl = '/operation/insert';
    $.post( sUrl , oParams ,function( result ){
        //console.log(result);
        if(result.code == '0'){
            alert('插入成功');
            var opTable = $("#operation-table").DataTable();
            opTable.draw(true);
        }
        else
        {
            alert("插入失败 " + result.message);
        }

    });
}

function deleteOperation(id,index)
{
    $.get("/operation/delete?operationId="+id,function(result) {
        var opTable = $("#operation-table").DataTable();
        opTable.rows(index).remove().draw(true);
    });
}
// 操作列表
$(function()
{
    var operationTableOption = createCommonTableOptions();
    operationTableOption.ajax = {
        // url可以直接指定远程的json文件，或是MVC的请求地址 /Controller/Action
        url: "/operation/query",
        type: 'POST',
        // 异步调用
        async:true,
        // 传给服务器的数据，可以添加我们自己的查询参数
        data: function (param)
        {
            param.start = param.start;
            param.offset = param.length;
            switch (param.order[0].column)
            {
                case 1:
                    param.sortPro = "createtime";
                    break;
                case 2:
                    param.sortPro = "excutetime";
                    break;
            }

            if("asc" == param.order[0].dir)
            {
                param.sortType = 0;
            }
            else if("desc" == param.order[0].dir)
            {
                param.sortType = 1;
            }
            return param;
        },
        //用于处理服务器端返回的数据。 dataSrc是DataTable特有的
        dataFilter: function (myJson)
        {
            var result = JSON.parse(myJson);
            var retStr = JSON.stringify(result.data);
            return  retStr;
        }
    };

    operationTableOption.columns = [
        { "data": "operationid","orderable": false },
        { "data": "createtime" ,
            "orderable": true,
            "render": function ( data, type, row, meta )
            {
                if(data > 0)
                    return $.myTime.UnixToDate(data,true)
                else
                    return ""
            }
        },
        { "data": "excutetime" ,
            "orderable": true,
            "render": function ( data, type, row, meta )
            {
                if(data > 0)
                    return $.myTime.UnixToDate(data,true)
                else
                    return ""
            }},
        { "data": "needuploadlocation","orderable": false,
            "render": function ( data, type, row, meta )
            {
                if(data)
                {
                    return "是";
                }
                else
                {
                    return "否";
                }
            }
        },
        { "data": "recordsbegindate","orderable": false },
        { "data": "recordsenddate" ,"orderable": false},
        { "data": "uploadsoundsids","orderable": false },
        {"render":function ( data, type, row, meta )
        {

            var operationId = row.operationid;
            return "<button type=\"button\"onclick=\"deleteOperation("+ operationId +"," +meta.row+ ")\">删除</button>";
        }}
    ];

    $("table.operation-table").DataTable(operationTableOption);

    // websocket
    socketId = getRnd5();
    initWebsocket(socketId);
})

// 重写websocket 客户端接受函数
function onWebsockMessage(msg)
{
    var bean = JSON.parse(msg);

    if(bean.type == 1)
    {
        // 如果是有新的位置消息上传，更新下对应的operation状态
        // 根据operationId选择行
        var row = $("table.operation-table").DataTable()
            .row( function ( idx, data, node ) {

                return data.operationId == bean.data.operationId ?

                    true : false;

            } );
        row.data().excutetime = $.myTime.UnixToDate(bean.data.excutetime,true);
        row.draw();
    }
}
